package com.jingdianjichi.subject.domain.handler.subject;

import com.jingdianjichi.subject.common.enums.IsDeleteFlagEnum;
import com.jingdianjichi.subject.common.enums.SubjectInfoTypeEnum;
import com.jingdianjichi.subject.domain.convert.RadioSubjectConverter;
import com.jingdianjichi.subject.domain.entity.SubjectAnswerBO;
import com.jingdianjichi.subject.domain.entity.SubjectInfoBO;
import com.jingdianjichi.subject.domain.entity.SubjectOptionBO;
import com.jingdianjichi.subject.domain.handler.SubjectTypeHandler;
import com.jingdianjichi.subject.infra.basic.entity.SubjectRadio;
import com.jingdianjichi.subject.infra.basic.service.SubjectRadioService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Component
public class RedioTypeHandler implements SubjectTypeHandler {
    @Resource
    private SubjectRadioService subjectRadioService;

    @Override
    public SubjectInfoTypeEnum getHandlerType() {
        return SubjectInfoTypeEnum.RADIO;
    }

    @Override
    public void add(SubjectInfoBO subjectInfoBo) {

        //单选题目插入
        List<SubjectRadio> subjectRadio = new ArrayList<>();
        if (!subjectInfoBo.getOptionList().isEmpty()) {
            subjectInfoBo.getOptionList().forEach(option-> {
                SubjectRadio answerRadio = RadioSubjectConverter.INSTANCE.converBoToEntity(option);
//                SubjectRadio radio = new SubjectRadio();
                Integer optionType = option.getOptionType();
                String optionContent = option.getOptionContent();
                Integer isCorrect = option.getIsCorrect();
                answerRadio.setOptionType(optionType);
                answerRadio.setOptionContent(optionContent);
                answerRadio.setIsCorrect(isCorrect);
                answerRadio.setSubjectId(subjectInfoBo.getId());
                answerRadio.setIsDelete(IsDeleteFlagEnum.UN_DELETED.getCode());
                subjectRadio.add(answerRadio);
            });
        }
        subjectRadioService.batchInster(subjectRadio);
    }

    @Override
    public SubjectOptionBO query(Integer subjectId) {
        SubjectRadio radio = new SubjectRadio();
        radio.setSubjectId(subjectId);
        List<SubjectRadio> subjectRadiosList =  subjectRadioService.queryBySubjectId(radio);
        List<SubjectAnswerBO> answerBoList = RadioSubjectConverter.INSTANCE.convertToBO(subjectRadiosList);
        SubjectOptionBO optionBo = new SubjectOptionBO();
        optionBo.setOptionList(answerBoList);
        return optionBo;
    }
}
